load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin")
load("//tools:defaults.bzl", "jasmine_node_test", "pkg_npm", "ts_library")

package(default_visibility = ["//visibility:public"])

copy_to_bin(
    name = "collection_assets",
    srcs = [
        "collection.json",
        "migration.json",
    ],
)

copy_to_bin(
    name = "ng_generate_assets",
    srcs = glob([
        "ng-generate/*/files/**/*",
    ]),
)

copy_to_bin(
    name = "schema_assets",
    srcs = glob([
        "**/schema.json",
    ]),
)

ts_library(
    name = "schematics",
    srcs = glob(
        ["**/*.ts"],
        exclude = [
            "**/*.spec.ts",
            "paths.ts",
            "ng-generate/*/files/**/*.ts",
        ],
    ),
    # Schematics can not yet run in ESM module. For now we continue to use CommonJS.
    # TODO(ESM): remove this once the Angular CLI supports ESM schematics.
    devmode_module = "commonjs",
    prodmode_module = "commonjs",
    tsconfig = ":tsconfig.json",
    deps = [
        "//src/cdk/schematics",
        "@npm//@angular-devkit/core",
        "@npm//@angular-devkit/schematics",
        "@npm//@schematics/angular",
        # TODO(devversion): Only include jasmine for test sources (See: tsconfig types).
        "@npm//@types/jasmine",
        "@npm//@types/node",
        "@npm//rxjs",
        "@npm//tslint",
        "@npm//typescript",
    ],
)

ts_library(
    name = "paths",
    testonly = True,
    srcs = ["paths.ts"],
    data = [
        ":collection_assets",
    ],
    # Schematics can not yet run in ESM module. For now we continue to use CommonJS.
    # TODO(ESM): remove this once the Angular CLI supports ESM schematics.
    devmode_module = "commonjs",
    prodmode_module = "commonjs",
    tsconfig = ":tsconfig.json",
    deps = [
        "@npm//@bazel/runfiles",
    ],
)

# This package is intended to be combined into the main @angular/material package as a dep.
pkg_npm(
    name = "npm_package",
    srcs = ["package.json"],
    nested_packages = [
        "//src/material/schematics/ng-generate/theme-color:npm_package",
    ],
    deps = [
        ":collection_assets",
        ":ng_generate_assets",
        ":schema_assets",
        ":schematics",
        "//src/material/schematics/ng-update:ng_update_index",
    ],
)

### Testing rules

jasmine_node_test(
    name = "unit_tests",
    srcs = [":unit_test_sources"],
    data = [
        ":collection_assets",
        ":ng_generate_assets",
        ":schema_assets",
        ":schematics_test_cases",
    ],
)

ts_library(
    name = "unit_test_sources",
    testonly = True,
    srcs = glob(
        ["**/*.spec.ts"],
        exclude = [
            "**/*.e2e.spec.ts",
            "ng-generate/*/files/**/*.spec.ts",
        ],
    ),
    # Schematics can not yet run in ESM module. For now we continue to use CommonJS.
    # TODO(ESM): remove this once the Angular CLI supports ESM schematics.
    devmode_module = "commonjs",
    prodmode_module = "commonjs",
    tsconfig = ":tsconfig.json",
    deps = [
        ":paths",
        ":schematics",
        "//src/cdk/schematics",
        "//src/cdk/schematics/testing",
        "@npm//@angular-devkit/core",
        "@npm//@angular-devkit/schematics",
        "@npm//@schematics/angular",
        "@npm//@types/fs-extra",
        "@npm//@types/jasmine",
        "@npm//@types/node",
        "@npm//fs-extra",
    ],
)

filegroup(
    name = "schematics_test_cases",
    testonly = True,
    srcs = glob([
        "ng-update/test-cases/**/*_input.ts",
        "ng-update/test-cases/**/*_expected_output.ts",
    ]),
)
